

package com.yeyks.modules.sys.controller;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.dal.domain.SysMenu;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.common.exception.RRException;
import com.yeyks.common.utils.Constant;
import com.yeyks.common.utils.Result;
import com.yeyks.commonReference.service.account.SysMenuService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 系统菜单
 *
 * @author administrator
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@Login
	@RequestMapping(value = "/nav", method = RequestMethod.POST)
	public Result nav(@ApiIgnore @LoginUser AccountInfo user){
		List<SysMenu> menuList = sysMenuService.getUserMenuList(user.getId());
		return Result.ok().put("menuList", menuList);
	}
	
	/**
	 * 所有菜单列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:list")
	public List<SysMenu> list(@RequestParam(name = "token",required = true) String token){
		List<SysMenu> menuList = sysMenuService.list();
		for(SysMenu SysMenu : menuList){
			SysMenu parentMenuEntity = sysMenuService.getById(SysMenu.getParentId());
			if(parentMenuEntity != null){
				SysMenu.setParentName(parentMenuEntity.getName());
			}
		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:select")
	public Result select(@RequestParam(name = "token",required = true) String token){
		//查询列表数据
		List<SysMenu> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenu root = new SysMenu();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return Result.ok().put("menuList", menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping(value = "/info/{menuId}", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:info")
	public Result info(@PathVariable("menuId") Long menuId,@RequestParam(name = "token",required = true) String token){
		SysMenu menu = sysMenuService.getById(menuId);
		return Result.ok().put("menu", menu);
	}
	
	/**
	 * 保存
	 */
	//@SysLog("保存菜单")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:save")
	public Result save(@RequestBody SysMenu menu,@RequestParam(name = "token",required = true) String token){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.save(menu);
		
		return Result.ok();
	}
	
	/**
	 * 修改
	 */
	//@SysLog("修改菜单")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:update")
	public Result update(@RequestBody SysMenu menu,@RequestParam(name = "token",required = true) String token){
		//数据校验
		verifyForm(menu);
				
		sysMenuService.updateById(menu);
		
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	//@SysLog("删除菜单")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:delete")
	public Result delete(@RequestParam(name = "menuId", defaultValue = "", required = true)long menuId,@RequestParam(name = "token",required = true) String token){
		if(menuId <= 31){
			return Result.error("系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenu> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return Result.error("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return Result.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenu menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new RRException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenu parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
